package com.ac.scum.log.task.mapper.player;

import com.ac.scum.log.bean.Player;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.stream.Collectors;

@Mapper
public interface PlayerMapper extends BaseMapper<Player> {


    @Select("select p.*,pjs.server_id,pjs.fake_name,pjs.name,pjs.join_time,pjs.account_number from player p left join server_player pjs on pjs.player_steam_id = p.steam_id where pjs.server_id = #{serverId} and p.steam_id in (${steamIds})")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "avatar", column = "avatar"),
            @Result(property = "steamId", column = "steam_id"),
            @Result(property = "serverId", column = "server_id"),
            @Result(property = "fakeName", column = "fake_name"),
            @Result(property = "joinTime", column = "join_time"),
            @Result(property = "steamName", column = "steam_name"),
            @Result(property = "accountNumber", column = "account_number"),
            @Result(property = "createTime", column = "create_time"),
    })
    List<Player> findPlayerByServerIdAndSteamIdStr(@Param("serverId") Long serverId, @Param("steamIds") String steamIds);

    default List<Player> findPlayerByServerIdAndSteamIds(Long serverId, List<Long> steamIds) {
        if (steamIds.isEmpty()) {
            return Lists.newArrayList();
        }
        return findPlayerByServerIdAndSteamIdStr(serverId, steamIds.stream().map(Object::toString).collect(Collectors.joining(",")));
    }

    @Insert("insert into server_player value (#{steamId},#{serverId},#{fakeName},#{name},#{accountNumber},#{joinTime})")
    int joinServer(Player player);

    @Select("select count(*) from server_player where player_steam_id = #{steamId} and server_id = #{serverId}")
    int selectCountByServerIdAndSteamId(@Param("serverId") Long serverId, @Param("steamId") Long steamId);

    default void insertAndJoinServer(Player player) {
        if (selectCount(Wrappers.lambdaQuery(Player.class).eq(Player::getSteamId, player.getSteamId())) == 0) {
            insert(player);
        }
        if (selectCountByServerIdAndSteamId(player.getServerId(), player.getSteamId()) == 0){
            joinServer(player);
        }
    }

    @Update("update server_player set name = #{name} where server_id = #{serverId} and player_steam_id = #{steamId}")
    void updateServerPlayerName(@Param("serverId") Long serverId, @Param("steamId") Long steamId, @Param("name") String name);

    @Update("update server_player set name = #{name},account_number = #{accountNumber} where server_id = #{serverId} and player_steam_id = #{steamId}")
    void updateServerPlayerNameAndAccountNumber(@Param("serverId") Long serverId, @Param("steamId") Long steamId, @Param("name") String name, @Param("accountNumber") String accountNumber);
}
